import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class RW extends GameObj {
    // 方向left,right
    private String direction = "NULL";
    private static final int LEFT_BOUNDARY = 10;
    private static final int RIGHT_BOUNDARY = 1890;

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public RW(Image img, int x, int y, GameWin frame) {
        super(img, x, y, frame);
        // 键盘监听事件
        this.frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                changeDirection(e);
            }
        });
    }

    // 获取当前应该使用的图片级别 (1-8)
    private int getImageLevel() {
        if (GameWin.dragonBallCount < 7) return 1;
        return Math.min((GameWin.dragonBallCount / 7) + 1, 8);
    }

    // 获取向右的图片
    private Image getRightImage() {
        switch (getImageLevel()) {
            case 1: return GameUtils.Img;    // 1.png
            case 2: return GameUtils.Img2;   // 2.png
            case 3: return GameUtils.Img3;   // 3.png
            case 4: return GameUtils.Img4;   // 4.png
            case 5: return GameUtils.Img5;   // 5.png
            case 6: return GameUtils.Img6;   // 6.png
            case 7: return GameUtils.Img7;   // 7.png
            case 8: return GameUtils.Img8;   // 8.png
            default: return GameUtils.Img;
        }
    }

    // 获取向左的图片
    private Image getLeftImage() {
        switch (getImageLevel()) {
            case 1: return GameUtils.zImg;
            case 2: return GameUtils.zImg2;
            case 3: return GameUtils.zImg3;
            case 4: return GameUtils.zImg4;
            case 5: return GameUtils.zImg5;
            case 6: return GameUtils.zImg6;
            case 7: return GameUtils.zImg7;
            case 8: return GameUtils.zImg8;
            default: return GameUtils.zImg;
        }
    }

    // 控制方向a-left  d-right
    public void changeDirection(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_A) { // 左
            img = getLeftImage();
            x -= width;
        }
        if (e.getKeyCode() == KeyEvent.VK_D) { // 右
            img = getRightImage();
            x += width;
        }
    }

    // 人物移动
    public void move() {
        switch (direction) {
            case "left":
                x -= width;
                break;
            case "right":
                x += width;
            default:
                break;
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);

        // 食物碰撞检测
        FoodObj food = this.frame.foodObj;
        if (food != null) {
            // 计算角色和食物的矩形区域
            Rectangle characterRect = new Rectangle(x, y, 114, 150);
            Rectangle foodRect = new Rectangle(food.x, food.y, food.width, food.height);

            // 检测矩形是否相交（碰撞）
            if (characterRect.intersects(foodRect)) {
                // 根据食物类型处理不同效果
                handleFoodCollision(food);
                // 获取新食物
                this.frame.foodObj = food.getFood();
            }
        }
        //通关判定
        if (GameWin.score>=100){
            //通关
            GameWin.state=4;
        }
        if (GameWin.score<0){
            //失败
            GameWin.state=3;
        }


        move();

        // 边界检查
        if (x < 0) {
            x = RIGHT_BOUNDARY;
        } else if (x > RIGHT_BOUNDARY) {
            x = LEFT_BOUNDARY;
        }
    }

    // 处理食物碰撞效果
    private void handleFoodCollision(FoodObj food) {
        if (food.getImg() == GameUtils.kxImg) {
            // 碰到扣分食物，得分-1
            GameWin.score = Math.max(-1, GameWin.score - 1); // 确保分数不低于0
        } else if (food.getImg() == GameUtils.lzImg) {
            // 碰到龙珠食物，龙珠+1
            GameWin.dragonBallCount++;
        } else if (food.getImg() == GameUtils.swImg) {
            // 碰到普通食物，根据龙珠数量计算得分
            int multiplier = GameWin.dragonBallCount / 7;
            int scoreToAdd = Math.max(1, multiplier * 2); // 至少加1分，每7个龙珠多+2分
            GameWin.score += scoreToAdd;
        }
    }
}



